// Copyright 2012 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package adwords.axis.v201206.basicoperations;

import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.axis.v201206.cm.AdServingOptimizationStatus;
import com.google.api.ads.adwords.axis.v201206.cm.Budget;
import com.google.api.ads.adwords.axis.v201206.cm.BudgetBudgetDeliveryMethod;
import com.google.api.ads.adwords.axis.v201206.cm.BudgetBudgetPeriod;
import com.google.api.ads.adwords.axis.v201206.cm.Campaign;
import com.google.api.ads.adwords.axis.v201206.cm.CampaignOperation;
import com.google.api.ads.adwords.axis.v201206.cm.CampaignReturnValue;
import com.google.api.ads.adwords.axis.v201206.cm.CampaignServiceInterface;
import com.google.api.ads.adwords.axis.v201206.cm.CampaignStatus;
import com.google.api.ads.adwords.axis.v201206.cm.FrequencyCap;
import com.google.api.ads.adwords.axis.v201206.cm.GeoTargetTypeSetting;
import com.google.api.ads.adwords.axis.v201206.cm.GeoTargetTypeSettingPositiveGeoTargetType;
import com.google.api.ads.adwords.axis.v201206.cm.KeywordMatchSetting;
import com.google.api.ads.adwords.axis.v201206.cm.Level;
import com.google.api.ads.adwords.axis.v201206.cm.ManualCPC;
import com.google.api.ads.adwords.axis.v201206.cm.ManualCPM;
import com.google.api.ads.adwords.axis.v201206.cm.Money;
import com.google.api.ads.adwords.axis.v201206.cm.NetworkSetting;
import com.google.api.ads.adwords.axis.v201206.cm.Operator;
import com.google.api.ads.adwords.axis.v201206.cm.Setting;
import com.google.api.ads.adwords.axis.v201206.cm.TargetRestrictSetting;
import com.google.api.ads.adwords.axis.v201206.cm.TimeUnit;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.common.lib.auth.ClientLoginTokens;

import org.joda.time.DateTime;

/**
 * This example adds campaigns.
 *
 * Credentials and properties in {@code fromFile()} are pulled from the
 * "ads.properties" file. See README for more info.
 *
 * Tags: CampaignService.mutate
 *
 * @category adx-exclude
 * @author Adam Rogal
 */
public class AddCampaigns {

  public static void main(String[] args) throws Exception {
    // Get a ClientLogin AuthToken.
    String clientLoginToken = new ClientLoginTokens.Builder()
        .forApi(ClientLoginTokens.Api.ADWORDS)
        .fromFile()
        .build()
        .requestToken();

    // Construct an AdWordsSession.
    AdWordsSession session =
        new AdWordsSession.Builder().fromFile().withClientLoginToken(clientLoginToken).build();

    AdWordsServices adWordsServices = new AdWordsServices();

    runExample(adWordsServices, session);
  }

  public static void runExample(
      AdWordsServices adWordsServices, AdWordsSession session) throws Exception {
    // Get the CampaignService.
    CampaignServiceInterface campaignService =
        adWordsServices.get(session, CampaignServiceInterface.class);

    // Create campaign.
    Campaign campaign = new Campaign();
    campaign.setName("Interplanetary Cruise #" + System.currentTimeMillis());
    campaign.setStatus(CampaignStatus.PAUSED);
    campaign.setBiddingStrategy(new ManualCPC());

    // You can optionally provide these field(s).
    campaign.setStartDate(new DateTime().plusDays(1).toString("yyyyMMdd"));
    campaign.setStartDate(new DateTime().plusDays(30).toString("yyyyMMdd"));
    campaign.setAdServingOptimizationStatus(AdServingOptimizationStatus.ROTATE);
    campaign.setFrequencyCap(new FrequencyCap(5L, TimeUnit.DAY, Level.ADGROUP));

    // Create budget.
    Budget budget = new Budget();
    budget.setPeriod(BudgetBudgetPeriod.DAILY);
    budget.setAmount(new Money(null, 50000000L));
    budget.setDeliveryMethod(BudgetBudgetDeliveryMethod.STANDARD);
    campaign.setBudget(budget);

    // Set the campaign network options to Search and Search Network.
    NetworkSetting networkSetting = new NetworkSetting();
    networkSetting.setTargetGoogleSearch(true);
    networkSetting.setTargetSearchNetwork(true);
    networkSetting.setTargetContentNetwork(false);
    networkSetting.setTargetPartnerSearchNetwork(false);
    campaign.setNetworkSetting(networkSetting);

    // Set options that are not required.
    GeoTargetTypeSetting geoTarget = new GeoTargetTypeSetting();
    geoTarget.setPositiveGeoTargetType(GeoTargetTypeSettingPositiveGeoTargetType.DONT_CARE);
    KeywordMatchSetting keywordMatch = new KeywordMatchSetting();
    keywordMatch.setOptIn(Boolean.FALSE);
    // Display network targeting settings (non-required).
    // Defaults to TRUE for new Campaigns, but can be set to TRUE for existing
    // campaigns like below.
    TargetRestrictSetting targetRestrict = new TargetRestrictSetting();
    targetRestrict.setUseAdGroup(Boolean.TRUE);
    campaign.setSettings(new Setting[] {geoTarget, keywordMatch, targetRestrict});

    // You can create multiple campaigns in a single request.
    Campaign campaign2 = new Campaign();
    campaign2.setName("Interplanetary Cruise banner #" + System.currentTimeMillis());
    campaign2.setStatus(CampaignStatus.PAUSED);
    campaign2.setBiddingStrategy(new ManualCPM());

    Budget budget2 = new Budget();
    budget2.setPeriod(BudgetBudgetPeriod.DAILY);
    budget2.setAmount(new Money(null, 150000000L));
    budget2.setDeliveryMethod(BudgetBudgetDeliveryMethod.STANDARD);
    campaign2.setBudget(budget2);

    NetworkSetting networkSetting2 = new NetworkSetting();
    networkSetting2.setTargetGoogleSearch(false);
    networkSetting2.setTargetSearchNetwork(false);
    networkSetting2.setTargetContentNetwork(true);
    networkSetting2.setTargetPartnerSearchNetwork(false);
    campaign2.setNetworkSetting(networkSetting2);

    KeywordMatchSetting keywordMatch2 = new KeywordMatchSetting();
    keywordMatch2.setOptIn(Boolean.FALSE);
    campaign2.setSettings(new Setting[] {keywordMatch2});

    // Create operations.
    CampaignOperation operation = new CampaignOperation();
    operation.setOperand(campaign);
    operation.setOperator(Operator.ADD);
    CampaignOperation operation2 = new CampaignOperation();
    operation2.setOperand(campaign2);
    operation2.setOperator(Operator.ADD);

    CampaignOperation[] operations = new CampaignOperation[] {operation, operation2};

    // Add campaigns.
    CampaignReturnValue result = campaignService.mutate(operations);

    // Display campaigns.
    for (Campaign campaignResult : result.getValue()) {
      System.out.println("Campaign with name \"" + campaignResult.getName() + "\" and id \""
          + campaignResult.getId() + "\" was added.");
    }
  }
}
